package controllers;

import java.util.Collections;
import java.util.List;

import play.mvc.*;
import models.*;

public class Security extends Secure.Security {

	/**
	 * Authenticate an user with his password and email
	 * @param email
	 * @param password
	 * @return
	 */
	static boolean authenticate(String email, String password) {
        User user = User.find("byEmail", email).first();
        return user != null && user.checkPassword(password);
    } 
	
	/**
	 * Check whether the current connected user is an administrator
	 * @return
	 */
	public static boolean connectedUserIsAdmin() {
        User user = User.find("byEmail", connected()).first();
        return user instanceof models.Admin;
    }
	
	/**
	 * Go to the main page when an user logs out
	 */
	static void onDisconnected() {       
		Application.index(null, null, null);
	}
	
	/**
	 * Go to the main page when an user logs in
	 */
	static void onAuthenticated() {
		Application.index(null, null, null);
    }
	
	/**
	 * Get the currently connected user
	 * @return the user if one is connected, null else
	 */
	public static User getConnected() {
		return User.find("byEmail", Security.connected()).first();
	}

}